PhP Login/Register system [migrated]

Posted by Marian on Pro Webmasters See other posts from Pro Webmasters or by Marian
Published on 2012-03-23T13:52:06Z Indexed on 2012/03/23 17:41 UTC
Read the original article Hit count: 348

Filed under:
|
|

I found this good tutorial on creating a login/register system using PhP and MySQL. The forum is around 5 years old (edited last year) but it can still be usefull.

Beginner Simple Register-Login system

There seems to be an issue with both login and register pages.

<?php 

function register_form(){ 

$date = date('D, M, Y'); 
echo "<form action='?act=register' method='post'>" 
."Username: <input type='text' name='username' size='30'><br>" 
."Password: <input type='password' name='password' size='30'><br>" 
."Confirm your password: <input type='password' name='password_conf' size='30'><br>" 
."Email: <input type='text' name='email' size='30'><br>" 
."<input type='hidden' name='date' value='$date'>" 
."<input type='submit' value='Register'>" 
."</form>"; 

} 

function register(){ 

$connect = mysql_connect("host", "username", "password"); 
if(!$connect){ 
die(mysql_error());
} 

$select_db = mysql_select_db("database", $connect); 
if(!$select_db){ 
die(mysql_error()); 
} 

$username = $_REQUEST['username']; 
$password = $_REQUEST['password']; 
$pass_conf = $_REQUEST['password_conf']; 
$email = $_REQUEST['email']; 
$date = $_REQUEST['date']; 


if(empty($username)){ 
die("Please enter your username!<br>"); 
} 

if(empty($password)){ 
die("Please enter your password!<br>"); 
} 

if(empty($pass_conf)){ 
die("Please confirm your password!<br>"); 
} 

if(empty($email)){ 
die("Please enter your email!"); 
} 


$user_check = mysql_query("SELECT username FROM users WHERE username='$username'"); 
$do_user_check = mysql_num_rows($user_check); 


$email_check = mysql_query("SELECT email FROM users WHERE email='$email'"); 
$do_email_check = mysql_num_rows($email_check); 


if($do_user_check > 0){ 
die("Username is already in use!<br>"); 
} 

if($do_email_check > 0){ 
die("Email is already in use!"); 
} 


if($password != $pass_conf){ 
die("Passwords don't match!"); 
} 


$insert = mysql_query("INSERT INTO users (username, password, email) VALUES      ('$username', '$password', '$email')"); 
if(!$insert){ 
die("There's little problem: ".mysql_error()); 
} 

echo $username.", you are now registered. Thank you!<br><a href=login.php>Login</a> |     <a href=index.php>Index</a>"; 

} 

switch($act){ 

default; 
register_form(); 
break; 

case "register"; 
register(); 
break; 

} 

?>

Once pressed the register button the page does nothing, fields are erased and no data is added inside the database or error given. I tought that the problem might be the switch($act){ part so I removed it and changed the page using a require

require('connect.php');

where connect.php is

<?php
mysql_connect("localhost","host","password");
mysql_select_db("database");
?>

Removed the function register_form(){ and echo part turning it into an HTML code:

<form action='register' method='post'> 
Username: <input type='text' name='username' size='30'><br>
Password: <input type='password' name='password' size='30'><br>
Confirm your password: <input type='password' name='password_conf' size='30'><br> 
Email: <input type='text' name='email' size='30'><br> 
<input type='hidden' name='date' value='$date'>
<input type='submit' name="register" value='Register'> 
</form>

And instead of having a function register(){ I replaced it with a if($register){

So when the Register button is pressed it runs the php code, but this edit doesn't seem to work either. So what can the problem be? If needed I can re-add this code on my Domain

The login page has the same issue, nothing happens when the button is pressed beside emptying the fields.

© Pro Webmasters or respective owner

Related posts about php

Related posts about login